package com.bupt.controller.system;

import com.bupt.annotation.AccessLimit;
import com.bupt.constants.ResponseResult;
import com.bupt.dto.sys.PageInfo;
import com.bupt.dto.sys.UserDto;
import com.bupt.exception.BaseException;
import com.bupt.service.project.NewProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(tags = {"项目的基本操作"})
@RequestMapping("/api")
public class NewProjectController {

    @Resource
    private NewProjectService newProjectService;

    @ApiOperation("创建项目")
    @AccessLimit(interfaceName = "创建项目")
    @PostMapping("/createNewProject")
    public Object createNewProject(@ApiIgnore UserDto userDto,
                                   HttpServletRequest request,
                                   @RequestParam("projectName")String projectName,
                                   @RequestParam("path")String path){

    try{
        Object project = newProjectService.createDemandProject(userDto,request, projectName, path);
        return ResponseResult.createOK(project);
    }catch (BaseException e){
        return ResponseResult.createFail(e.getMessage());
    }
    }

    @ApiOperation("删除项目")
    @AccessLimit(interfaceName = "删除项目")
    @GetMapping("/deleteProject/{pId}")
    public Object deleteProject(@ApiIgnore UserDto userDto,@PathVariable("pId") String pId){
        try{
            Object o = newProjectService.deleteDemandProject(userDto, pId);
            return ResponseResult.createOK(o);
        }catch (BaseException e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("查询某个项目的全部信息")
    @AccessLimit(interfaceName = "查询某个项目的全部信息")
    @GetMapping("/getProjects/{pId}")
    public Object getDemandProject(@ApiIgnore UserDto userDto,@PathVariable("pId")String pId){
        try{
            Object o = newProjectService.getDemandProject(userDto, pId);
            return  ResponseResult.createOK(o);
        }catch (BaseException e){
            return  ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("查询所有项目")
    @AccessLimit(interfaceName = "查询所有项目信息")
    @GetMapping("/projects")
    public Object getDemandProjects(@ApiIgnore UserDto userDto,HttpServletRequest request){
        try{
           return  ResponseResult.createOK(newProjectService.getDemandProjects(userDto,request));
        }catch (BaseException e){
            return ResponseResult.createFail(e.getMessage());
        }
    }
}
